package com.example.myapplication10;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookingDetailActivity extends AppCompatActivity {

    private Map<Integer, Button> timeButtons = new HashMap<>();
    private int selectedTimeId = -1;
    public static Map<String, List<String>> bookingRecords = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booking_detail);

        String counselorName = getIntent().getStringExtra("counselorName");
        TextView tvCounselorName = findViewById(R.id.tvCounselorName);
        TextView tvCounselorDesc = findViewById(R.id.tvCounselorDesc);
        tvCounselorName.setText(counselorName);
        setCounselorDescription(counselorName, tvCounselorDesc);
        initTimeButtons();
        Button btnBook = findViewById(R.id.btnBook);
        btnBook.setOnClickListener(v -> {
            if (selectedTimeId != -1) {
                EditText etName = findViewById(R.id.etName);
                EditText etPhone = findViewById(R.id.etPhone);
                String name = etName.getText().toString();
                String phone = etPhone.getText().toString();

                if (name.isEmpty() || phone.isEmpty()) {
                    Toast.makeText(this, "请填写基本信息", Toast.LENGTH_SHORT).show();
                    return;
                }

                String selectedTime = timeButtons.get(selectedTimeId).getText().toString();
                String record = "姓名: " + name + ", 电话: " + phone + ", 时间: " + selectedTime;

                if (!bookingRecords.containsKey(counselorName)) {
                    bookingRecords.put(counselorName, new java.util.ArrayList<>());
                }
                bookingRecords.get(counselorName).add(record);

                Toast.makeText(this, "已成功预约" + selectedTime + "的咨询", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "请先选择预约时间", Toast.LENGTH_SHORT).show();
            }
        });
        Button btnViewRecords = findViewById(R.id.btnViewRecords);
        btnViewRecords.setOnClickListener(v -> {
            Intent intent = new Intent(BookingDetailActivity.this, BookingRecordActivity.class);
            startActivity(intent);
        });
    }

    private void setCounselorDescription(String counselorName, TextView descriptionView) {
        switch (counselorName) {
            case "张医生 - 抑郁症专家":
                descriptionView.setText("张医生拥有10年临床经验，擅长抑郁症、情绪障碍的诊断与治疗...");
                break;
            case "李医生 - 焦虑症专家":
                descriptionView.setText("李医生专注于焦虑症、强迫症等神经症的治疗...");
                break;
            case "王医生 - 青少年心理专家":
                descriptionView.setText("王医生是青少年心理问题专家，擅长处理青春期叛逆...");
                break;
            case "赵医生 - 婚姻家庭咨询师":
                descriptionView.setText("赵医生专注于婚姻家庭咨询，擅长处理夫妻关系、婆媳矛盾...");
                break;
            case "陈医生 - 创伤治疗专家":
                descriptionView.setText("陈医生是创伤治疗专家，擅长处理PTSD、童年创伤等问题...");
                break;
            case "刘医生 - 睡眠障碍专家":
                descriptionView.setText("刘医生专注于睡眠障碍的诊断与治疗...");
                break;
            case "杨医生 - 亲子关系专家":
                descriptionView.setText("杨医生擅长处理亲子关系问题...");
                break;
            case "吴医生 - 职场心理专家":
                descriptionView.setText("吴医生专注于职场心理问题的咨询与治疗...");
                break;
            case "周医生 - 老年心理专家":
                descriptionView.setText("周医生擅长处理老年心理问题...");
                break;
            case "孙医生 - 社交恐惧症专家":
                descriptionView.setText("孙医生专注于社交恐惧症的治疗...");
                break;
            default:
                descriptionView.setText("该咨询师拥有丰富的临床经验，专注于心理健康领域的治疗与咨询。");
        }
    }

    private void initTimeButtons() {
        // 绑定所有时间选择按钮并设置点击事件
        setupTimeButton(R.id.time_slot_1);
        setupTimeButton(R.id.time_slot_2);
        setupTimeButton(R.id.time_slot_3);
        setupTimeButton(R.id.time_slot_4);
        setupTimeButton(R.id.time_slot_5);
    }

    private void setupTimeButton(int buttonId) {
        Button button = findViewById(buttonId);
        timeButtons.put(buttonId, button);

        button.setOnClickListener(v -> {
            if (selectedTimeId != -1) {
                timeButtons.get(selectedTimeId).setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
            }
            selectedTimeId = buttonId;
            button.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));
        });
    }
}